From fc2058b4b6a64d66fe1ee318bccea42b4569d31f Mon Sep 17 00:00:00 2001
From: Alexander Polakov <polachok@gmail.com>
Date: Sun, 29 May 2011 19:23:17 +0400
Subject: [PATCH 7/8] ksh/vi: compensate for cursor move on command mode

 * move completion cursor one position right if the
   character is space
 * when we enter command mode, cursor is moved one
   position left, and the space on the end is "lost".
   So we are trying to complete "ls" instead of file
   while having "ls " and "file.c" when having "file.c ".
---
 vi.c |   15 ++++++++++++++-
 1 files changed, 14 insertions(+), 1 deletions(-)

diff --git vi.c vi.c
index 95d192c..0bac6be 100644
--- vi.c
+++ vi.c
@@ -1956,6 +1956,7 @@ complete_word(int command, int count, int flags)
 	int match_len;
 	int is_unique;
 	int is_command;
+	int pos;
 
 	/* Undo previous completion */
 	if (command == 0 && expanded == COMPLETE && buf) {
@@ -1974,11 +1975,23 @@ complete_word(int command, int count, int flags)
 		buf = 0;
 	}
 
+	/* XXX: hack. When we enter command mode, the cursor is moved
+	 * one position left. This means that the space at the end is
+	 * eaten and file completion becomes command completion.
+	 * (see x_locate_word() for more on this)
+	 */
+	pos = es->cursor;
+	if (command) {
+		pos += (isspace(es->cbuf[es->cursor]) ? 1 : 0);
+		if (pos > es->linelen)
+			pos = es->linelen;
+	}
+
 	/* XCF_FULLPATH for count 'cause the menu printed by print_expansions()
 	 * was done this way.
 	 */
 	nwords = x_cf_glob(XCF_COMMAND_FILE | (count ? XCF_FULLPATH : 0) | flags,
-	    es->cbuf, es->linelen, es->cursor,
+	    es->cbuf, es->linelen, pos,
 	    &start, &end, &words, &is_command);
 	if (nwords == 0) {
 		vi_error();
-- 
1.7.5

